<?php

class VendorController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        return View::make('vendors.index');
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
        return View::make('vendors.create');
	}


    /**
     * Get vendors from database and return json formatted for the datatable plugin
     * @return Datatable view(json)
     */
    public  function Datatable(){
        $vendors = Vendor::select(array('vendors.id','vendors.name','vendors.address','vendors.contact'));
        return Datatables::of($vendors)->add_column('operations','<a href="{{ URL::to( \'vendors\', array( $id,\'edit\' )) }}">Edit</a>
                <a href="{{ URL::to( \'vendors\', array($id,\'delete\')) }}">Delete</a>
            ')->make();
    }


    /**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
        // validate
        $rules = array(
            'name'       => 'required',
            'address'       => 'required',
            'contact'       => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('vendors/create')
                ->withErrors($validator)
                ->withInput();
            Input::flash();
        } else {
            // store
            $vendor = new Vendor();
            $vendor->name = Input::get('name');
            $vendor->address = Input::get('address');
            $vendor->contact = Input::get('contact');
            $vendor->save();

            // redirect
            Session::flash('message', 'Successfully added a vendor');
            return Redirect::to('vendors');
        }
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}